home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.3 / scandir.3 < prev    next >
Text File  |  1995-07-27  |  6KB  |  199 lines

  1.  
  2.  
  3.  
  4.      SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))                UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV                SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           scandir, alphasort - scan a directory
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ttttyyyyppppeeeessss....hhhh>>>>
  13.           ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ddddiiiirrrr....hhhh>>>>
  14.  
  15.           iiiinnnntttt
  16.           ssssccccaaaannnnddddiiiirrrr((((nnnnaaaammmmeeee,,,, lllliiiisssstttt,,,, sssseeeelllleeeeccccttttoooorrrr,,,, ssssoooorrrrtttteeeerrrr))))
  17.               cccchhhhaaaarrrr ****nnnnaaaammmmeeee;;;;
  18.               ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ************lllliiiisssstttt;;;;
  19.               iiiinnnntttt ((((****sssseeeelllleeeeccccttttoooorrrr))))(((())));;;;
  20.               iiiinnnntttt ((((****ssssoooorrrrtttteeeerrrr))))(((())));;;;
  21.  
  22.           iiiinnnntttt
  23.           aaaallllpppphhhhaaaassssoooorrrrtttt((((dddd1111,,,, dddd2222))))
  24.               ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ********dddd1111;;;;
  25.               ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ********dddd2222;;;;
  26.  
  27.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  28.           _S_c_a_n_d_i_r reads the directory _n_a_m_e and builds a
  29.           NULL-terminated array of pointers to the entries found in
  30.           that directory.  This array is put into the location pointed
  31.           to by the _l_i_s_t parameter.
  32.  
  33.           If the _s_e_l_e_c_t_o_r parameter is non-NULL, it is taken to be a
  34.           pointer to a function called with each entry, to determine
  35.           whether or not it should be included in the returned list.
  36.           If the parameter is NULL, all entries are included.
  37.  
  38.           As an added feature, the entries can be sorted (with
  39.           _q_s_o_r_t(3)) before the list is returned.  If the _s_o_r_t_e_r
  40.           parameter is non-NULL, it is passed to qsort to use as the
  41.           comparison function.  The _a_l_p_h_a_s_o_r_t routine is provided to
  42.           sort the array alphabetically.
  43.  
  44.           The array pointed to by _l_i_s_t and the items it points to are
  45.           all space obtained through _m_a_l_l_o_c(3), and their storage can
  46.           be reclaimed as shown in the example below.
  47.  
  48.      EEEEXXXXAAAAMMMMPPPPLLLLEEEE
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 7/27/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))                UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV                SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
  71.  
  72.  
  73.  
  74.           Here is a small _l_s(1)-like program:
  75.                #include <stdio.h>
  76.                #include <sys/types.h>
  77.                #include <sys/stat.h>
  78.                #include <sys/dir.h>
  79.  
  80.                extern int alphasort();
  81.  
  82.                static int
  83.                filesonly(e)
  84.                     struct direct *e;
  85.                {
  86.                     struct stat sb;
  87.  
  88.                     return(stat(e->d_name, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG);
  89.                }
  90.  
  91.                main(ac, av)
  92.                     int ac;
  93.                     char *av[];
  94.                {
  95.                     register int i;
  96.                     register int j;
  97.                     struct direct **list;
  98.  
  99.                     if (ac != 2) {
  100.                          fprintf(stderr, "usage: %s dirname0, av[0]);
  101.                          exit(1);
  102.                     }
  103.                     if (chdir(av[1]) < 0) {
  104.                          perror(av[1]);
  105.                          exit(1);
  106.                     }
  107.                     if ((i = scandir(".", &list, filesonly, alphasort)) < 0) {
  108.                          perror("Error reading directory");
  109.                          exit(1);
  110.                     }
  111.                     for (j = 0; j < i; j++)
  112.                          printf("%s0, list[j]->d_name);
  113.                     for (j = 0; j < i; j++)
  114.                          free((char *)list[j]);
  115.                     free((char *)list);
  116.                     exit(0);
  117.                }
  118.  
  119.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  120.           directory(3), qsort(3)
  121.  
  122.      DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
  123.           Returns the number of entries in the ``list,'' or -1 if the
  124.           directory could not be opened or a memory allocation failed.
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 7/27/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))                UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV                SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
  137.  
  138.  
  139.  
  140.      BBBBUUUUGGGGSSSS
  141.           The routine can be slightly wasteful of space.
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 7/27/95)
  196.  
  197.  
  198.  
  199.